<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Dialogs</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, IronPython, Winforms, dialogs, programming, tutorial, Linux">
<meta name="description" content="Dialogs">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href=".." title="Home">Contents</a>


<h1>Dialogs</h1>


<p>
In this part of the IronPython Mono Winforms tutorial, we will talk about dialogs.
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
Dialog windows or dialogs are an indispensable part of most modern GUI applications. 
A dialog is defined as a conversation between two or more persons. In a computer 
application a dialog is a window which is used to "talk" to the application. 
A dialog is used to input data, modify data, change the application settings etc. 
Dialogs are important means of communication between a user and a computer program.
</p>
<p>
There are essentially two types of dialogs. Predefined dialogs and custom dialogs. 
</p>


<h2>FolderBrowserDialog</h2>

<p>
This dialog prompts the user to select a folder. 
</p>

<div class="codehead">folderbrowserdialog.py</div>
<pre class="code">
#!/usr/bin/ipy

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Windows.Forms import Application, Form, StatusBar
from System.Windows.Forms import ToolBar, ToolBarButton, FolderBrowserDialog
from System.Windows.Forms import DialogResult



class IForm(Form):

    def __init__(self):
        self.Text = "FolderBrowserDialog"
 
        toolbar = ToolBar()
        toolbar.Parent = self
        openb = ToolBarButton()

        self.statusbar = StatusBar()
        self.statusbar.Parent = self

        toolbar.Buttons.Add(openb)
        toolbar.ButtonClick += self.OnClicked

        self.CenterToScreen()


    def OnClicked(self, sender, event):
        dialog = FolderBrowserDialog()

        if (dialog.ShowDialog(self) == DialogResult.OK):
            self.statusbar.Text = dialog.SelectedPath
       

Application.Run(IForm())
</pre>

<p>
We have a toolbar and one toolbar button. By clicking on the button, 
the <b class="keyword">FolderBrowserDialog</b> appears on the screen. 
The name of the selected folder is shown in the statusbar. 
</p>


<pre class="explanation">
 dialog = FolderBrowserDialog()
</pre>

<p>
The <b class="keyword">FolderBrowserDialog</b> is created.
</p>

<pre class="explanation">
 if (dialog.ShowDialog(self) == DialogResult.OK):
     self.statusbar.Text = dialog.SelectedPath  
</pre>

<p>
The <b class="keyword">ShowDialog()</b> method shows the dialog on the screen. 
If we click on the OK button of the dialog, the selected directory path is shown on the statusbar.
</p>


<br>
<img src="/img/gui/ironpython/folderbrowser.png" alt="FolderBrowserDialog">
<div class="figure">Figure: FolderBrowserDialog</div>


<h2>ColorDialog</h2>

<p>
This dialog displays available colors along with controls that enable 
the user to define custom colors. 
</p>

<div class="codehead">colordialog.py</div>
<pre class="code">
#!/usr/bin/ipy

import sys
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Windows.Forms import Application, Form, ToolBar
from System.Windows.Forms import ToolBarButton, ControlStyles, ColorDialog
from System.Windows.Forms import DialogResult
from System.Drawing import Size, Color, SolidBrush, Rectangle

RECT_WIDTH  = 100
RECT_HEIGHT = 100

class IForm(Form):

    def __init__(self):
        self.Text = "ColorDialog"

        self.color = Color.Blue

        toolbar = ToolBar()
        toolbar.Parent = self
        openb = ToolBarButton()

        toolbar.Buttons.Add(openb)
        toolbar.ButtonClick += self.OnClicked

        self.LocateRect()

        self.SetStyle(ControlStyles.ResizeRedraw, True)
        self.Paint += self.OnPaint
 
        self.CenterToScreen()
    
    
    def OnPaint(self, event):
        g = event.Graphics
        self.LocateRect()
        brush = SolidBrush(self.color)
        g.FillRectangle(brush, self.r)
    

    def OnClicked(self, sender, events):
        dialog = ColorDialog()

        if (dialog.ShowDialog(self) == DialogResult.OK):
            self.color = dialog.Color
            self.Invalidate()
       
    def LocateRect(self):
        x = (self.ClientSize.Width - RECT_WIDTH) / 2
        y = (self.ClientSize.Height - RECT_HEIGHT) / 2
        self.r = Rectangle(x, y, RECT_WIDTH, RECT_HEIGHT)
    
    
Application.Run(IForm())
</pre>

<p>
In this code example, we use the <b class="keyword">ColorDialog</b> to choose a color 
for a rectangle, that is located in the middle of the form control. 
</p>

<pre class="explanation">
 self.color = Color.Blue
</pre>

<p>
At the beginning, the color of the rectangle is blue. We use the 
<b class="keyword">self.color</b> variable to determine the color of the rectangle. 
</p>

<pre class="explanation">
 dialog = ColorDialog()
</pre>

<p>
The <b class="keyword">ColorDialog</b> is created. 
</p>

<pre class="explanation">
 if (dialog.ShowDialog(self) == DialogResult.OK):
     self.color = dialog.Color
     self.Invalidate()
</pre>

<p>
The code shows the color dialog. If we click on the OK button, we get 
the selected color and call the <b class="keyword">Invalidate()</b> method. 
The method invalidates the entire surface of the control and causes the control 
to be redrawn. The result is that the rectangle is drawn with a new color value.
</p>


<br>
<img src="/img/gui/ironpython/colordialog.png" alt="ColorDialog">
<div class="figure">Figure: ColorDialog</div>


<h2>FontDialog</h2>

<p>
The <b class="keyword">FontDialog</b> is used to select fonts.
</p>

<div class="codehead">fontdialog.py</div>
<pre class="code">
#!/usr/bin/ipy

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Windows.Forms import Application, Form, StatusBar, Label
from System.Windows.Forms import ToolBar, ToolBarButton, FontDialog
from System.Windows.Forms import DialogResult

class IForm(Form):

    def __init__(self):
        self.Text = "FolderBrowserDialog"
 
        self.text = Label()
        self.text.Parent = self
        self.text.Text = "Winforms tutorial"

        self.LocateText()
 
        toolbar = ToolBar()
        toolbar.Parent = self
        openb = ToolBarButton()

        toolbar.Buttons.Add(openb)
        toolbar.ButtonClick += self.OnClicked

        self.text.AutoSize = True
        self.Resize += self.OnResize
 
        self.CenterToScreen()
    

    def OnResize(self, sender, event):
        self.LocateText()


    def LocateText(self): 
        self.text.Top = (self.ClientSize.Height - self.text.Height) / 2
        self.text.Left = (self.ClientSize.Width - self.text.Width) / 2
    

    def OnClicked(self, sender, event):

        dialog = FontDialog()

        if (dialog.ShowDialog(self) == DialogResult.OK):
           self.text.Font = dialog.Font
           self.LocateText()
       

Application.Run(IForm())
</pre>

<p>
We draw some text in the middle of the form control. We use the font 
dialog to change font for this text. 
</p>

<pre class="explanation">
 dialog = FontDialog()
</pre>

<p>
<b class="keyword">FontDialog</b> is created. 
</p>

<pre class="explanation">
 if (dialog.ShowDialog(self) == DialogResult.OK):
     self.text.Font = dialog.Font
     self.LocateText()
</pre>

<p>
When we click on the OK button, we set a newly selected font for the <b class="keyword">Label</b>
control. Because the size of the text changes with different fonts, we must call 
the <b class="keyword">LocateText()</b> method, which locates the text in the middle of the form control. 
</p>

<br>
<img src="/img/gui/ironpython/fontdialog.png" alt="FontDialog">
<div class="figure">Figure: FontDialog</div>


<h2>OpenDialog</h2>

<p>
This dialog is used to open files.
</p>

<div class="codehead">opendialog.py</div>
<pre class="code">
#!/usr/bin/ipy

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Windows.Forms import Application, Form, TextBox
from System.Windows.Forms import ToolBar, ToolBarButton, OpenFileDialog
from System.Windows.Forms import DialogResult, ScrollBars, DockStyle


class IForm(Form):

    def __init__(self):
        self.Text = "OpenDialog"
 
        toolbar = ToolBar()
        toolbar.Parent = self
        openb = ToolBarButton()
        

        self.textbox = TextBox()
        self.textbox.Parent = self
        self.textbox.Multiline = True
        self.textbox.ScrollBars = ScrollBars.Both
        self.textbox.WordWrap = False
        self.textbox.Parent = self
        self.textbox.Dock = DockStyle.Fill
      

        toolbar.Buttons.Add(openb)
        toolbar.ButtonClick += self.OnClicked


        self.CenterToScreen()

    def OnClicked(self, sender, event):
        dialog = OpenFileDialog()
        dialog.Filter = "C# files (*.cs)|*.cs"

        if dialog.ShowDialog(self) == DialogResult.OK:
            f = open(dialog.FileName)
            data = f.read()
            f.Close()
            self.textbox.Text = data
       

Application.Run(IForm())
</pre>

<p>
We use the <b class="keyword">OpenDialog</b> control to open C# source files. 
We have a <b class="keyword">TextBox</b> control, where we display the file. 
</p>


<pre class="explanation">
 dialog = OpenFileDialog()
</pre>

<p>
The <b class="keyword">OpenDialog</b> is created. 
</p>


<pre class="explanation">
 dialog.Filter = "C# files (*.cs)|*.cs"
</pre>

<p>
We set the <b class="keyword">Filter</b> property to C# source files. 
Only C# files can be chosen with this dialog instance. 
</p>

<pre class="explanation">
 if dialog.ShowDialog(self) == DialogResult.OK:
     f = open(dialog.FileName)
     data = f.read()
     f.Close()
     self.textbox.Text = data
</pre>

<p>
After clicking OK, we read the contents of the chosen file and put it into 
the <b class="keyword">TextBox</b> control. 
</p>

<br>
<img src="/img/gui/ironpython/opendialog.png" alt="OpenDialog">
<div class="figure">Figure: OpenDialog</div>

<hr class="btm">

<p>
In this part of the IronPython Mono Winforms tutorial, we showed various dialogs. 
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified January 29, 2009  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

